package com.example.project.controller;


import com.example.project.dto.TcdmSecureHazardAssessmentRulesDTO;
import com.example.project.entity.TcdmSecureHazardAssessmentRules;
import com.example.project.service.TcdmSecureHazardAssessmentRulesService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * (TcdmSecureHazardAssessmentRules)表控制层
 *
 * @author makejava
 * @since 2024-02-23 15:18:23
 */
@RestController
@CrossOrigin
@RequestMapping("tcdmSecureHazardAssessmentRules")
public class TcdmSecureHazardAssessmentRulesController {
    /**
     * 服务对象
     */
    @Resource
    private TcdmSecureHazardAssessmentRulesService tcdmSecureHazardAssessmentRulesService;

    /**
     * 分页查询
     *
     * @param tcdmSecureHazardAssessmentRulesDTO 筛选条件
     * @return 查询结果
     */

    @PostMapping("findPage")
    public ResponseEntity<Page<TcdmSecureHazardAssessmentRules>> queryByPage(@RequestBody TcdmSecureHazardAssessmentRulesDTO tcdmSecureHazardAssessmentRulesDTO) throws ParseException {
        com.example.project.entity.Page page=tcdmSecureHazardAssessmentRulesDTO.getPage();
        Date end=null;
        Date star=null;
        if(tcdmSecureHazardAssessmentRulesDTO.getMyTime2().getEndTime()!=null&&tcdmSecureHazardAssessmentRulesDTO.getMyTime2().getEndTime()!=""){
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            end=format.parse(tcdmSecureHazardAssessmentRulesDTO.getMyTime2().getEndTime());
        }
        if(tcdmSecureHazardAssessmentRulesDTO.getMyTime2().getStartTime()!=null&&tcdmSecureHazardAssessmentRulesDTO.getMyTime2().getStartTime()!=""){
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            star =format.parse(tcdmSecureHazardAssessmentRulesDTO.getMyTime2().getStartTime());

        }
        return ResponseEntity.ok(this.tcdmSecureHazardAssessmentRulesService.queryByPage(tcdmSecureHazardAssessmentRulesDTO.getTcdmSecureHazardAssessmentRules(),PageRequest.of(page.getPage(), page.getSize()) ,star,end));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */

    @GetMapping("{id}")
    public ResponseEntity<TcdmSecureHazardAssessmentRules> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.tcdmSecureHazardAssessmentRulesService.queryById(id));
    }

    /**
     * 查询所有数据
     *
     * @return 所有数据
     */
    @GetMapping("findAll")
    public List<TcdmSecureHazardAssessmentRules> findAll() {
        return tcdmSecureHazardAssessmentRulesService.findList();
    }
    /**
     * 新增数据
     *
     * @param tcdmSecureHazardAssessmentRules 实体
     * @return 新增结果
     */

    @PostMapping("add")
    public ResponseEntity<TcdmSecureHazardAssessmentRules> add(@RequestBody TcdmSecureHazardAssessmentRules tcdmSecureHazardAssessmentRules) {
        return ResponseEntity.ok(this.tcdmSecureHazardAssessmentRulesService.insert(tcdmSecureHazardAssessmentRules));
    }

    /**
     * 编辑数据
     *
     * @param tcdmSecureHazardAssessmentRules 实体
     * @return 编辑结果
     */

    @PutMapping("put")
    public ResponseEntity<TcdmSecureHazardAssessmentRules> edit(@RequestBody TcdmSecureHazardAssessmentRules tcdmSecureHazardAssessmentRules) {
        return ResponseEntity.ok(this.tcdmSecureHazardAssessmentRulesService.update(tcdmSecureHazardAssessmentRules));
    }

    /**
     * 删除数据
     *
     * @param tcdmSecureHazardAssessmentRules 通过获取序号来删除
     * @return 删除是否成功
     */
    @DeleteMapping("del")
    public ResponseEntity<Boolean> deleteById(@RequestBody TcdmSecureHazardAssessmentRules tcdmSecureHazardAssessmentRules) {
        return ResponseEntity.ok(this.tcdmSecureHazardAssessmentRulesService.deleteById(tcdmSecureHazardAssessmentRules.getTsharId()));
    }


    @CrossOrigin
    @PostMapping("queryByCondition")
    public List<TcdmSecureHazardAssessmentRules> queryByCondition(@RequestBody TcdmSecureHazardAssessmentRules tcdmSecureHazardAssessmentRules){
        return this.tcdmSecureHazardAssessmentRulesService.queryCondition(tcdmSecureHazardAssessmentRules);
    }

}

